/*
 * Copyright (c) 2007, 2008 fr3@K <freak@fsfoundry.org>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef GION__AUTO_HANDLE_HPP
#define GION__AUTO_HANDLE_HPP

#include <algorithm> // std::swap

namespace gion
{

  namespace bits
  {
    template <class HandleTraits>
    struct auto_handle_ref
    {
      typedef HandleTraits traits_type;
      typedef typename HandleTraits::handle_type handle_type;

      explicit auto_handle_ref(handle_type)
      : handle(handle)
      {}
    
      handle_type handle;
    };
  } // namespace bits

  template <class HandleTraits>
  class auto_handle
  {
  public:
    typedef HandleTraits traits_type;
    typedef typename HandleTraits::handle_type handle_type;

    explicit auto_handle(
      handle_type handle = traits_type::invalid_handle())
      : handle_(handle)
    {
    }
    auto_handle(auto_handle& other)
      : handle_(other.release())
    {
    }
    auto_handle& operator=(auto_handle& other)
    {
      this->reset(other.release());
      return *this;
    }
    auto_handle(bits::auto_handle_ref<traits_type> ref)
      : handle_(ref.handle)
    {
    }
    auto_handle& operator=(bits::auto_handle_ref<traits_type> ref)
    {
      this->reset(ref.handle);
      return *this;
    }
    ~auto_handle()
    {
      this->reset();
    }

    operator bits::auto_handle_ref<traits_type>()
    {
      return bits::auto_handle_ref<traits_type>(this->release());
    }

    handle_type get() const
    {
      return handle_;
    }

    handle_type release()
    {
      handle_type ret(handle_);
      handle_ = traits_type::invalid_handle();
      return ret;
    }

    void reset(handle_type handle = traits_type::invalid_handle())
    {
      if(handle_ != handle)
      {
        if(handle_ != traits_type::invalid_handle())
          traits_type::deallocate(this->release());
        handle_ = handle;
      }
    }

    void swap(auto_handle& other)
    {
      using std::swap;
      swap(handle_, other.handle_);
    }

    static handle_type invalid_handle()
    {
      return traits_type::invalid_handle();
    }

  private:
    handle_type handle_;
  };

  template <class HandleTraits>
  void swap(
    auto_handle<HandleTraits>& lhs,
    auto_handle<HandleTraits>& rhs)
  {
    lhs.swap(rhs);
  }

} // namespace gion

#endif // !GION__AUTO_HANDLE_HPP
